package com.fuhao.weixin.service;

import com.alibaba.fastjson.JSONObject;
import com.fuhao.core.Constants.Constants;
import com.fuhao.core.base.BaseApiService;
import com.fuhao.core.base.BaseResponse;
import com.fuhao.core.utils.RedisUtil;
import com.fuhao.weixin.Service.VerificaCodeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhang
 * @title: VerificaCodeServiceImpl
 */
@RestController
public class VerificaCodeServiceImpl extends BaseApiService<JSONObject> implements VerificaCodeService {
        @Autowired
        private RedisUtil redisUtil;

        @Override
        public BaseResponse<JSONObject> verificaWeixinCode(String phone, String weixinCode) {
            if (StringUtils.isEmpty(phone)) {
                return setResultError("手机号码不能为空!");
            }
            if (StringUtils.isEmpty(weixinCode)) {
                return setResultError("注册码不能为空!");
            }
            String key = Constants.WEIXINCODE_KEY + phone;
            String code = redisUtil.getString(key);
            if (StringUtils.isEmpty(code)) {
                return setResultError("注册码错误或已经过期,请重新发送验证码");
            }
            if (!code.equals(weixinCode)) {
                return setResultError("注册码不正确");
            }
            redisUtil.delKey(key);
            return setResultSuccess("注册码验证码正确");
        }
}
